package com.wehealth.three.lib_connect.device.usbdevice

import android.content.Context
import android.hardware.usb.UsbManager
import android.os.Handler
import android.os.Message
import com.wehealth.three.lib_connect.device.usbdevice.data.ParseOriginEcgData
import com.wehealth.three.lib_connect.device.usbdevice.data.ParseOriginEcgData.EcgDataGetListener
import com.wehealth.three.lib_connect.device.usbdevice.driver.UsbSerialDriver
import com.wehealth.three.lib_connect.device.usbdevice.port.UsbSerialPort
import com.wehealth.three.lib_connect.utils.AppGlobals
import com.wehealth.three.lib_connect.utils.PreferenceUtils

/**
 * @desc 定义通用的usb操作
 * @author 栾桂明
 * @date 2023年12月18日
 */
class UsbDeviceHelper(
    usbSerialDriver: UsbSerialDriver,
    ecgDataListener: EcgDataGetListener,
    val handler: Handler
) : DeviceOperationCommand() {
    companion object {
        const val USB_OPERATION_TIMEOUT_MILLIS = 500
        const val USB_OPERATION_START_GET_DEVICE_INFO = 100000
        const val USB_OPERATION_START_GET_DEVICE_INFO_FAILURE = 100001
        const val USB_OPERATION_START_GET_DEVICE_INFO_SUCCESS = 100002
    }
    private val usbManager = AppGlobals.get()?.getSystemService(Context.USB_SERVICE) as UsbManager

    private val usbSerialPort = usbSerialDriver.ports.first()

    private val usbConnection = usbManager.openDevice(usbSerialDriver.device)

    private val ecgDataParse: ParseOriginEcgData

    init {
        //配置串口参数
        usbSerialPort.setParameters(921600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE)
        usbSerialPort.dtr = true
        ecgDataParse = ParseOriginEcgData(ecgDataListener)
    }

    override fun sendStart(startCommand: ByteArray) {
        usbSerialPort.write(startCommand, USB_OPERATION_TIMEOUT_MILLIS)
    }

    override fun sendEnd(command: ByteArray) {
        usbSerialPort.write(endCommand, USB_OPERATION_TIMEOUT_MILLIS)
    }

    override fun getDeviceStatus(command: ByteArray) {
        usbSerialPort.write(command, USB_OPERATION_TIMEOUT_MILLIS)
    }

    override fun getDeviceInfo(command: ByteArray) {
        usbSerialPort.write(command, USB_OPERATION_TIMEOUT_MILLIS)
    }

    /***
     * 获取设备的序列号
     */
    override fun setDeviceSerialNumber() {
        //开始获取设备序列号
        handler.sendEmptyMessage(USB_OPERATION_START_GET_DEVICE_INFO)
        try {
            try {
                Thread.sleep(200)
            } catch (e: Exception) {
                val message = Message.obtain()
                message.what = USB_OPERATION_START_GET_DEVICE_INFO_FAILURE
                message.obj = "获取设备序列号失败"
                handler.sendMessage(message)
            }
            usbSerialPort.open(usbConnection)
            usbSerialPort.write(deviceInfoCommand, USB_OPERATION_TIMEOUT_MILLIS)
            val bufferData = ByteArray(31)
            usbSerialPort.read(bufferData, USB_OPERATION_TIMEOUT_MILLIS)
            if (!ecgDataParse.EcgParserCMDInfo(bufferData)) { //
                val msg = handler.obtainMessage(USB_OPERATION_START_GET_DEVICE_INFO_FAILURE)
                msg.obj = "获取设备序列号失败"
                handler.sendMessage(msg)
                return
            }
            val serialNo = PreferenceUtils.getInstance().tempSerialNo
            val serialDataMessage = Message.obtain()
            serialDataMessage.what = USB_OPERATION_START_GET_DEVICE_INFO_SUCCESS
            serialDataMessage.obj = serialNo
            handler.sendMessage(serialDataMessage)

        } catch (e: Exception) {
            val msg = handler.obtainMessage(USB_OPERATION_START_GET_DEVICE_INFO_FAILURE)
            msg.obj = "获取设备序列号失败"
            handler.sendMessage(msg)
        }


    }

    override fun getHardWareVersionCode(command: ByteArray) {
        usbSerialPort.write(command, USB_OPERATION_TIMEOUT_MILLIS)
    }

}
